home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3588 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.5 KB  |  85 lines

  1. Path: ts1-026.jaxnet.com!user
  2. From: garyg@jax.jaxnet.com (Gary M. Greenberg)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: [Q]How can I read ints and floats by using fgets() ?
  5. Date: Mon, 29 Jan 1996 23:51:33 -0500
  6. Organization: Southeast Network Services, Inc.
  7. Message-ID: <garyg-2901962351330001@ts1-026.jaxnet.com>
  8. References: <4e56sn$p6n@wraith.cc.uow.edu.au> <4e8i56$j4a@jaxnet.jaxnet.com>
  9. NNTP-Posting-Host: ts1-026.jaxnet.com
  10.  
  11. Some guy, garyg@jax.jaxnet.com, wrote:
  12.  
  13. >  Abimanju Manoharan (am@wraith.cc.uow.edu.au) wrote:
  14. >  : Hi everybody,
  15. >  : I have a small problem in my c program. I want to read a file for exe.
  16. >  : the file (file size is not constant):
  17. >  
  18. >  : 4 11
  19. >  : 2.5 4 13 3.7
  20. >  : 5 4 2.1
  21. >  : 2.4 
  22. >  
  23. >  : I have to read each line by using fgets(). then I have to convert this 
  24. >  : string. There are float values and int values are mixed.
  25. >  : Du you any idea? or du you have another way to read this?
  26. >  
  27. [snip ...]
  28.  
  29. Don't know happened to that reply, but there was nuthin' in it
  30. worth anything. Oh well, let's try again ...
  31.  
  32. This is different than my original answer, but I hope it illustrates how
  33. one might use fgets and scanf to determine the 'value' of input. This
  34. illustration takes input from stdin. Modifying it to use a file is trivial.
  35.  
  36. Comments, criticism, and flames by post or email.
  37.  
  38. /* fgets2scanf.c */
  39.  
  40. #include <stdio.h>
  41. #include <string.h>
  42.  
  43. #define LEN 256
  44.  
  45. int main ()
  46. {
  47.     char str[LEN], tmpstr[LEN];
  48.     float fl;
  49.     int i=0,j=0,pos=0,neg=0,not=0;
  50.     printf("Enter a line: ");
  51.     while(fgets(str,sizeof(str),stdin)!=NULL) {
  52.         /*** chop newline && NULL terminate string ***/
  53.         *(str+strlen(str)-1)='\0';
  54.         i=0;
  55.         while (i<=strlen(str)) {
  56.             if((str[i])!=' ' && i<strlen(str)) {
  57.                 tmpstr[j]=str[i];
  58.                 j++;
  59.             }
  60.             if(str[i]==' '||str[i]=='\0'||i==strlen(str)) {
  61.                 tmpstr[j]='\0';
  62.                 j=0;
  63.                 if (sscanf(tmpstr,"%f",&fl)==1)
  64.                     (fl>=0.0)?pos++:neg++;
  65.                 else {
  66.                     not++;
  67.                     fflush(stdout);
  68.                     }
  69.                 }
  70.             i++;
  71.         }
  72.         printf("Enter another line: ");
  73.     }
  74.     printf("Positive values: %d\n""Negative values: %d\n"
  75.     "Discarded values: %d\n",pos,neg,not);
  76.     return 0;
  77. }
  78. /*** end ***/
  79.  
  80. gary    /* the Sorcerer's Apprentice */
  81.  
  82. "Why do we have to hide from the police, Daddy?"
  83. "Because we use vi, honey. They use emacs."
  84. "Unless we're on a Mac, then we use BBEdit 'cause `It Doesn't Suck!'"
  85.